package com.kuang.sync.pfsiup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kuang.mapper.pfsi.AstPfsiDeviceBranchMapper;
import com.kuang.mapper.pfsi.AstPfsiDeviceOpenMapper;
import com.kuang.mapper.pfsi.AstPfsiDeviceRegisterMapper;
import com.kuang.mapper.pfsi.AstPfsiDeviceStateMsgMapper;
import com.kuang.pojo.AstManageDeviceRegister;
import com.kuang.pojo.pfsi.AstPfsiDeviceBranch;
import com.kuang.pojo.pfsi.AstPfsiDeviceOpen;
import com.kuang.pojo.pfsi.AstPfsiDeviceRegister;
import com.kuang.pojo.pfsi.AstPfsiDeviceStateMsg;
import com.kuang.sync.pfsidown.PfsiDownService;
import com.kuang.utils.MQTTConnect;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

@Service
public class PfsiUpServiceImpl implements PfsiUpService{

    @Resource
    AstPfsiDeviceRegisterMapper astPfsiDeviceRegisterMapper;

    @Resource
    AstPfsiDeviceOpenMapper astPfsiDeviceOpenMapper;

    @Resource
    AstPfsiDeviceStateMsgMapper astPfsiDeviceStateMsgMapper;

    @Resource
    AstPfsiDeviceBranchMapper astPfsiDeviceBranchMapper;

    @Resource
    PfsiDownService pfsiDownService;

    @Resource
    MQTTConnect mqttConnect;

    @Override
    public String pfsiDeviceRegisterSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstPfsiDeviceRegister astPfsiDeviceRegister = JSON.parseObject(JSON.toJSONString(jsonObject), AstPfsiDeviceRegister.class);
        //设备编码
        astPfsiDeviceRegister.setDevice_code(id);
        //主键
        astPfsiDeviceRegister.setAst_pfsi_device_register_id(String.valueOf(UUID.randomUUID()));
        astPfsiDeviceRegister.setCreate_time(new Date());
        astPfsiDeviceRegister.setCmd(cmd);
        astPfsiDeviceRegister.setVer(ver);

        int insert = astPfsiDeviceRegisterMapper.insert(astPfsiDeviceRegister);

        //这个地方不是实时的需要回馈给平台，平台返回给我数据我再去发送给这个主题
        try {
            String s = pfsiDownService.postPfsiDeviceRegisterSrv(id, preant);
        } catch (MqttException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public String pfsiDeviceOpenSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstPfsiDeviceOpen astPfsiDeviceOpen = JSON.parseObject(JSON.toJSONString(jsonObject), AstPfsiDeviceOpen.class);
        //设备编码
        astPfsiDeviceOpen.setDevice_code(id);
        //主键
        astPfsiDeviceOpen.setAst_pfsi_device_open_id(String.valueOf(UUID.randomUUID()));
        astPfsiDeviceOpen.setCreate_time(new Date());
        astPfsiDeviceOpen.setCmd(cmd);
        astPfsiDeviceOpen.setVer(ver);

        int insert = astPfsiDeviceOpenMapper.insert(astPfsiDeviceOpen);

        //发送返回讯息
        JSONObject toJson = new JSONObject();
        JSONObject data = new JSONObject();
        toJson.put("cmd",Integer.parseInt(cmd));
        toJson.put("ver",ver);
        data.put("state","1");
        toJson.put("data",data);
        try {
            boolean pub = mqttConnect.pub("/pfsi/down/"+id, JSON.toJSONString(toJson));//发布主题
        } catch (MqttException e) {
            e.printStackTrace();
        }

        pfsiDownService.postPfsiDeviceOpen(astPfsiDeviceOpen);

        return null;
    }

    @Override
    public String pfsiDeviceStateMsgSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstPfsiDeviceStateMsg astPfsiDeviceStateMsg = JSON.parseObject(JSON.toJSONString(jsonObject), AstPfsiDeviceStateMsg.class);
        //设备编码
        astPfsiDeviceStateMsg.setDevice_code(id);
        //主键
        astPfsiDeviceStateMsg.setAst_pfsi_device_state_msg_id(String.valueOf(UUID.randomUUID()));
        astPfsiDeviceStateMsg.setCreate_time(new Date());
        astPfsiDeviceStateMsg.setCmd(cmd);
        astPfsiDeviceStateMsg.setVer(ver);

        int insert = astPfsiDeviceStateMsgMapper.insert(astPfsiDeviceStateMsg);

        //发送返回讯息
        JSONObject toJson = new JSONObject();
        JSONObject data = new JSONObject();
        toJson.put("cmd",Integer.parseInt(cmd));
        toJson.put("ver",ver);
        data.put("state","1");
        toJson.put("data",data);
        try {
            boolean pub = mqttConnect.pub("/pfsi/down/"+id, JSON.toJSONString(toJson));//发布主题
        } catch (MqttException e) {
            e.printStackTrace();
        }

        pfsiDownService.postPfsiDeviceStateMsg(astPfsiDeviceStateMsg);

        return null;
    }

    @Override
    public String pfsiDeviceBranchSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject data= preant.getJSONObject("data");

        AstPfsiDeviceBranch astPfsiDeviceBranch = JSON.parseObject(JSON.toJSONString(data), AstPfsiDeviceBranch.class);
        //设备编码
        astPfsiDeviceBranch.setDevice_code(id);
        //主键
        astPfsiDeviceBranch.setAst_pfsi_device_branch_id(String.valueOf(UUID.randomUUID()));
        astPfsiDeviceBranch.setCreate_time(new Date());
        astPfsiDeviceBranch.setCmd(cmd);
        astPfsiDeviceBranch.setVer(ver);
        int insert = astPfsiDeviceBranchMapper.insert(astPfsiDeviceBranch);

        //发送返回讯息
        JSONObject toJson = new JSONObject();
        JSONObject toData = new JSONObject();
        toJson.put("cmd",Integer.parseInt(cmd));
        toJson.put("ver",ver);
        toData.put("state","1");
        toJson.put("data",toData);
        try {
            boolean pub = mqttConnect.pub("/pfsi/down/"+id, JSON.toJSONString(toJson));//发布主题
        } catch (MqttException e) {
            e.printStackTrace();
        }

        pfsiDownService.postPfsiDeviceBranch(astPfsiDeviceBranch);

        return null;
    }

    @Override
    public String postBranchMixingPump(String id, String json) {
        JSONObject preant = JSON.parseObject(json);

        //这个地方不是实时的需要回馈给平台，平台返回给我数据我再去发送给这个主题
        String s = pfsiDownService.postBranchMixingPump(id, preant);

        return null;
    }

    @Override
    public String postBranchInput(String id, String json) {
        JSONObject preant = JSON.parseObject(json);

        //这个地方不是实时的需要回馈给平台，平台返回给我数据我再去发送给这个主题
        String s = pfsiDownService.postBranchInput(id, preant);

        return null;
    }

    @Override
    public String postBranchOutput(String id, String json) {
        JSONObject preant = JSON.parseObject(json);

        //这个地方不是实时的需要回馈给平台，平台返回给我数据我再去发送给这个主题
        String s = pfsiDownService.postBranchOutput(id, preant);

        return null;
    }

    @Override
    public String postWaterOrFertiPump(String id, String json) {
        JSONObject preant = JSON.parseObject(json);

        //这个地方不是实时的需要回馈给平台，平台返回给我数据我再去发送给这个主题
        String s = pfsiDownService.postWaterOrFertiPump(id, preant);

        return null;
    }

    @Override
    public String postRemotePfsiValveResp(String id, String json) {
        JSONObject preant = JSON.parseObject(json);

        //这个地方不是实时的需要回馈给平台，平台返回给我数据我再去发送给这个主题
        String s = pfsiDownService.postRemotePfsiValveResp(id, preant);

        return null;
    }

    @Override
    public String posUpdatePfsiValveStatus(String id, String json) {
        JSONObject preant = JSON.parseObject(json);

        //这个地方不是实时的需要回馈给平台，平台返回给我数据我再去发送给这个主题
        String s = pfsiDownService.posUpdatePfsiValveStatus(id, preant);

        return null;
    }
}
